package SMSGUI;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Locale;

import jxl.CellView;
import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.format.UnderlineStyle;
import jxl.write.Formula;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;


public class writeExcel {

	private static WritableCellFormat timesBoldUnderline;
	private static WritableCellFormat times;

	public static void write(String inputFile, HashMap<String, String> data) throws IOException, WriteException {
		File file = new File(inputFile);
		WorkbookSettings wbSettings = new WorkbookSettings();

		wbSettings.setLocale(new Locale("en", "EN"));

		WritableWorkbook workbook = Workbook.createWorkbook(file, wbSettings);
		workbook.createSheet("Report", 0);
		WritableSheet excelSheet = workbook.getSheet(0);
		createLabel(excelSheet);
		createContent(excelSheet, data);

		workbook.write();
		workbook.close();
	}

	private static void createLabel(WritableSheet sheet)
			throws WriteException {
		// Lets create a times font
		WritableFont times10pt = new WritableFont(WritableFont.TIMES, 10);
		// Define the cell format
		times = new WritableCellFormat(times10pt);
		

		// Create create a bold font with unterlines
		WritableFont times10ptBoldUnderline = new WritableFont(
				WritableFont.TIMES, 10, WritableFont.BOLD, false,
				UnderlineStyle.SINGLE);
		timesBoldUnderline = new WritableCellFormat(times10ptBoldUnderline);
		

		CellView cv = new CellView();
		cv.setFormat(times);
		cv.setFormat(timesBoldUnderline);
		cv.setAutosize(true);

				

	}

	private static void createContent(WritableSheet sheet, HashMap<String, String> data) throws WriteException,
			RowsExceededException {
		// Now a bit of text
		Iterator<String> k = (Iterator<String>) data.keySet().iterator();
		int i = 0;
		while(k.hasNext())
		{
			String column1 = k.next();
			String column2 = data.get(column1);
			// First column
				addLabel(sheet, 0, i, column1);
				// Second column
				addLabel(sheet, 1, i, column2);
			i++;
		}
		
	}

		
	private static void addLabel(WritableSheet sheet, int column, int row, String s)
			throws WriteException, RowsExceededException {
		Label label;
		label = new Label(column, row, s, times);
		sheet.addCell(label);
	}

	public static void main(String[] args) throws WriteException, IOException {
		HashMap<String, String> data = new HashMap<String, String>();
		data.put("lol", "trol");
		data.put("trol" , " wattan? ");
		write("lars.xls", data);
		System.out
				.println("Please check the result file under c:/temp/lars.xls ");
	}
}